package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {

        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        //获取当前登录人id
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);

        //根据dishId和dishFlovor 或者 setmealId 查询购物车中有没有该菜品或者套餐
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.list(shoppingCart);

        //目标：添加菜品或者套餐到购物车表中
        if(shoppingCartList!=null && shoppingCartList.size()>0){
            //购物车中有该菜品或者套餐,修改数量+1  update shopping_cart number = number+1 where
            ShoppingCart cart = shoppingCartList.get(0);
            cart.setNumber(cart.getNumber()+1);

            shoppingCartMapper.updateNumberById(cart);
        }else{
            //购物车中有该菜品或者套餐，添加菜品或套餐到购物车中，数量为1 insert into shopping_cart
            Long dishId = shoppingCartDTO.getDishId();
            if(dishId!=null){
                //添加菜品到购物车中
                //根据菜品id查询菜品信息
                Dish dish = dishMapper.getById(dishId);

                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());

            }else{
                //添加套餐到购物车中
                Setmeal setmeal = setmealMapper.getById(shoppingCart.getSetmealId());

                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
            }
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            //添加菜品或者套餐到购物车
            shoppingCartMapper.insert(shoppingCart);
        }
    }

    @Override
    public List<ShoppingCart> list() {

        // ShoppingCart shoppingCart = new ShoppingCart();
        // shoppingCart.setUserId(BaseContext.getCurrentId());

        ShoppingCart shoppingCart = ShoppingCart.builder()
                                    .userId(BaseContext.getCurrentId())
                                    .build();

        return shoppingCartMapper.list(shoppingCart);
    }

    @Override
    public void clean() {
        //delete from shopping_cart where user_id= ？
        shoppingCartMapper.clean(BaseContext.getCurrentId());
    }
}
